package spuStoreWeighRecord

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/purchase/internal/svc"
	"insighttracking/apps/purchase/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type DeleteSpuStoreWeighRecordApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewDeleteSpuStoreWeighRecordApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *DeleteSpuStoreWeighRecordApiLogic {
	hostname, _ := os.Hostname()
	return &DeleteSpuStoreWeighRecordApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *DeleteSpuStoreWeighRecordApiLogic) DeleteSpuStoreWeighRecordApi(req *types.DeleteSpuStoreWeighRecordReq) (resp string, err error) {
	weighRecordEntity, err := service.SpuStoreWeighRecordService.GetSpuStoreWeighRecordByIdApi(l.ctx, req.ID)
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return "", errors.New("记录不存在")
	}
	if weighRecordEntity.Status == 1 {
		return "", errors.New("已完成的记录不能删除")
	}
	if err = service.SpuStoreWeighRecordService.DeleteSpuStoreWeighRecordApi(l.ctx, req.ID); err != nil {
		return "", errors.New("操作失败")
	}
	return "操作成功", nil
}
